/*
 * Copyright (c) 2022-2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef OHOS_LITE
#include <iostream>
#include <thread>
#include <chrono>

#include "gtest/gtest.h"
#include "i_player_engine.h"
#include "i_engine_factory.h"
#include "foundation/log.h"

using namespace OHOS::Media;

extern "C" {
__attribute__((visibility("default"))) OHOS::Media::IEngineFactory* CreateEngineFactory();
}

namespace OHOS::Media::Test {
class StTestStdSinglePlayer : public ::testing::Test {
    virtual void SetUp() override
    {
    }
    virtual void TearDown() override
    {
    }
};

static bool StartStdPlayer(const std::string& url, std::unique_ptr<IPlayerEngine>& player)
{
    std::cout << "Use media_standard interface player." << std::endl;
    auto engineFactory = std::unique_ptr<OHOS::Media::IEngineFactory>(CreateEngineFactory());
    player = engineFactory->CreatePlayerEngine();
    FALSE_RETURN_V(0 == player->SetSource(url), false);
    FALSE_RETURN_V(0 == player->SetLooping(false), false);
    FALSE_RETURN_V(0 == player->Prepare(), false);
    FALSE_RETURN_V(0 == player->Play(), false);
    return true;
}

TEST_F(StTestStdSinglePlayer, test_play_local_source_stop)
{
    std::unique_ptr<IPlayerEngine> player;
    ASSERT_TRUE(StartStdPlayer(RESOURCE_DIR "/MP3/MP3_LONG_48000_32.mp3", player));
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    ASSERT_EQ(0, player->Stop());
}

TEST_F(StTestStdSinglePlayer, test_play_http_source_finish_automatically)
{
    std::unique_ptr<IPlayerEngine> player;
    ASSERT_TRUE(StartStdPlayer("http://localhost/resource-src/media/MP3/MP3_48000_32_SHORT.mp3", player));
    std::this_thread::sleep_for(std::chrono::milliseconds(3000));
}
}
#endif
